/****************************************************
 * 创建人：  tanglei  
 * 创建时间: 2022/6/16/17:50
 * 项目名称：junit_test
 * 文件名称: ListUtil.java
 * 文件描述: @Description: 
 *
 * All rights Reserved, Designed By 投资交易团队
 * @Copyright:2016-2022
 *
 ********************************************************/
package com.springboot.util;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 包名称：  src.util
 * 类名称：ListUtil
 * 类描述：工具类
 * 创建人：  tanglei
 * 创建时间：2022/6/16 17:50
 */
@Slf4j
public class ListUtil {


    /**
     * @description:
     * @param collection
     * @param size
     * @return: java.util.List<java.util.List<T>>
     * @author: tanglei
     * @time: 2022/6/16 17:40
     */
    public static <T> void split(Collection<T> collection, int size, Consumer<T> consumer) {
        if (CollUtil.isEmpty(collection)) {
            return;
        }
        List<List<T>> result = new ArrayList<>();
        ArrayList<T> subList = new ArrayList<>(size);
        for (T t : collection) {
            if (subList.size() >= size) {
                result.add(subList);
                subList = new ArrayList<>(size);
            }
            subList.add(t);
        }
        result.add(subList);
        result.forEach(item -> consumer.accept(item));
        return;
    }

    public static <T, R> List<R> mapField(List<T> list, Function<? super T, R> mapper, Comparator<? super R> comparator) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<R> collect = list.stream()
                .map(mapper)
                .sorted(comparator)
                .distinct()
                .collect(Collectors.toList());
        return collect;
    }

    public static interface Consumer<T> {

        void accept(List<T> list);

    }
}

